home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_06 / 1106084b < prev    next >
Text File  |  1993-04-06  |  9KB  |  298 lines

  1.     /**********************************************
  2.     *
  3.     *   pixel_grow(...
  4.     *
  5.     *   The function grows regions.  It is similar
  6.     *   to the grow function in segment.c, but it
  7.     *   has several new capabilities.  It can
  8.     *   eliminate regions if they are too large or
  9.     *   too small.
  10.     *
  11.     *   It ignores pixels = FORGET_IT.  This allows
  12.     *   it to ignore edges or regions already
  13.     *   eliminated from consideration.
  14.     *
  15.     *   It adds pixels to a growing region only if
  16.     *   the pixel is close enough to the average gray
  17.     *   level of that region.
  18.     *
  19.     ***********************************************/
  20.  
  21. pixel_grow(input, output, diff, min_area, max_area)
  22.    short input[ROWS][COLS],
  23.          output[ROWS][COLS],
  24.          max_area,
  25.          min_area,
  26.          diff;
  27. {
  28.    char name[80];
  29.  
  30.    int count,
  31.        first_call,
  32.        i,
  33.        ii,
  34.        j,
  35.        jj,
  36.        object_found,
  37.        pointer,
  38.        pop_i,
  39.        pop_j,
  40.        stack_empty,
  41.        stack_file_in_use;
  42.  
  43.    short g_label, target, sum, stack[STACK_SIZE][2];
  44.  
  45.    for(i=0; i<ROWS; i++)
  46.       for(j=0; j<COLS; j++)
  47.          output[i][j] = 0;
  48.  
  49.    g_label       = 2;
  50.    object_found  = 0;
  51.    first_call    = 1;
  52.  
  53.             /*************************************
  54.             *
  55.             *   Now begin the process of growing
  56.             *   regions.
  57.             *
  58.             **************************************/
  59.  
  60.    for(i=0; i<ROWS; i++){
  61. if( (i%4) == 0) printf("\n");
  62. printf("-i=%3d label=%3d", i, g_label);
  63.       for(j=0; j<COLS; j++){
  64.  
  65.          target            = input[i][j];
  66.          sum               = target;
  67.          count             = 0;
  68.          stack_file_in_use = 0;
  69.          stack_empty       = 1;
  70.          pointer           = -1;
  71.  
  72.                /**********************************
  73.                *
  74.                *  Search for the first pixel of
  75.                *  a region.  It must not equal
  76.                *  FORGET_IT, and it must be close
  77.                *  enough to the target (ave value).
  78.                *
  79.                ***********************************/
  80.  
  81.          if(input[i][j] != FORGET_IT            &&
  82.             is_close(input[i][j], target, diff) &&
  83.             output[i][j] == 0){
  84.             pixel_label_and_check_neighbor(input,
  85.                            output, &target, &sum,
  86.                            &count, stack, g_label,
  87.                            &stack_empty, &pointer,
  88.                            i, j, diff,
  89.                            &stack_file_in_use,
  90.                            &first_call);
  91.             object_found = 1;
  92.          }  /* ends if is_close */
  93.                /*****************************
  94.                *
  95.                *  If the stack is not empty,
  96.                *  pop the coordinates of
  97.                *  the pixel off the stack
  98.                *  and check its 8 neighbors.
  99.                *
  100.                *******************************/
  101.  
  102.          while(stack_empty == 0){
  103.             pop_i = stack[pointer][0]; /* POP       */
  104.             pop_j = stack[pointer][1]; /* OPERATION */
  105.             --pointer;
  106.             if(pointer <= 0){
  107.                if(stack_file_in_use){
  108.                   pop_data_off_of_stack_file(
  109.                                  stack,
  110.                                  &pointer,
  111.                                  &stack_file_in_use);
  112.                }  /* ends if stack_file_in_use  */
  113.                else{
  114.                   pointer     = 0;
  115.                   stack_empty = 1;
  116.                }  /* ends else stack file is
  117.                      not in use  */
  118.             }  /*  ends if point <= 0  */
  119.             pixel_label_and_check_neighbor(input,
  120.                            output, &target, &sum,
  121.                            &count, stack, g_label,
  122.                            &stack_empty, &pointer,
  123.                            pop_i, pop_j,
  124.                            diff, &stack_file_in_use,
  125.                            &first_call);
  126.          }  /* ends while stack_empty == 0 */
  127.  
  128.          if(object_found == 1){
  129.             object_found = 0;
  130.  
  131.                   /**********************************
  132.                   *
  133.                   *  The object must be in the
  134.                   *  size constraints given by
  135.                   *  min_area and max_area
  136.                   *
  137.                   *********************************/
  138.  
  139.             if(count >= min_area  &&
  140.                count <= max_area)
  141.                ++g_label;
  142.                   /**********************************
  143.                   *
  144.                   *   Remove the object from the
  145.                   *   output.  Set all pixels in the
  146.                   *   object you are removing to
  147.                   *   FORGET_IT.
  148.                   *
  149.                   **********************************/
  150.  
  151.             else{
  152.                for(ii=0; ii<ROWS; ii++){
  153.                   for(jj=0; jj<COLS; jj++){
  154.                      if(output[ii][jj] == g_label){
  155.                         output[ii][jj] = 0;
  156.                         input[ii][jj]  = FORGET_IT;
  157.                      }  /* ends if output == g_label */
  158.                   }  /* ends loop over jj */
  159.                }  /* ends loop over ii */
  160.             }  /* ends else remove object */
  161.          }  /* ends if object_found == 1 */
  162.  
  163.       }   /* ends loop over j */
  164.    }  /* ends loop over i */
  165.  
  166.    printf("\nGROW> found %d objects", g_label);
  167.  
  168. } /* ends pixel_grow  */
  169.  
  170.  
  171.  
  172.  
  173.  
  174.    /********************************************
  175.    *
  176.    *  pixel_label_and_check_neighbors(...
  177.    *
  178.    *  This function labels a pixel with an object
  179.    *  label and then checks the pixel's 8
  180.    *  neighbors.  If any of the neigbors are
  181.    *  set, then they are also labeled.
  182.    *
  183.    *  It also updates the target or ave pixel
  184.    *  value of the pixels in the region being
  185.    *  grown.
  186.    *
  187.    ***********************************************/
  188.  
  189. pixel_label_and_check_neighbor(input_image,
  190.                          output_image, target,
  191.                          sum, count, stack,
  192.                          g_label, stack_empty,
  193.                          pointer, r, e, diff,
  194.                          stack_file_in_use,
  195.                          first_call)
  196. int   *count,
  197.       e,
  198.       *first_call,
  199.       *pointer,
  200.       r,
  201.       *stack_empty,
  202.       *stack_file_in_use;
  203.  
  204. short input_image[ROWS][COLS],
  205.       output_image[ROWS][COLS],
  206.       g_label,
  207.       *sum,
  208.       *target,
  209.       stack[STACK_SIZE][2],
  210.       diff;
  211. {
  212.    int already_labeled = 0,
  213.        i, j;
  214.  
  215.    if (output_image[r][e] != 0)
  216.       already_labeled = 1;
  217.  
  218.    output_image[r][e] = g_label;
  219.    *count  = *count + 1;
  220.    if(*count > 1){
  221.       *sum    = *sum + input_image[r][e];
  222.       *target = *sum / *count;
  223.    }
  224.  
  225.       /***************************************
  226.       *
  227.       *   Look at the 8 neighors of the
  228.       *   point r,e.
  229.       *
  230.       *   Ensure the points are close enough
  231.       *   to the target and do not equal
  232.       *   FORGET_IT.
  233.       *
  234.       *   Ensure the points you are checking
  235.       *   are in the image, i.e. not less
  236.       *   than zero and not greater than
  237.       *   ROWS-1 or COLS-1.
  238.       *
  239.       ***************************************/
  240.  
  241.    for(i=(r-1); i<=(r+1); i++){
  242.       for(j=(e-1); j<=(e+1); j++){
  243.  
  244.          if((i>=0)   &&
  245.             (i<=ROWS-1)  &&
  246.             (j>=0)   &&
  247.             (j<=COLS-1)){
  248.  
  249.             if( input_image[i][j] != FORGET_IT   &&
  250.                 is_close(input_image[i][j],
  251.                             *target, diff)       &&
  252.                 output_image[i][j] == 0){
  253.                *pointer           = *pointer + 1;
  254.                stack[*pointer][0] = i; /* PUSH      */
  255.                stack[*pointer][1] = j; /* OPERATION */
  256.                *stack_empty       = 0;
  257.  
  258.                if(*pointer >= (STACK_SIZE -
  259.                                STACK_FILE_LENGTH)){
  260.                   push_data_onto_stack_file(stack,
  261.                             pointer, first_call);
  262.                   *stack_file_in_use = 1;
  263.